package com.longrise.android.byjk.plugins.tabfirst;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.main.UpdataAPK;
import com.longrise.android.byjk.advertisement.InterstitialAD.InterstitialAD;
import com.longrise.android.byjk.advertisement.InterstitialAD.InterstitialADListener;
import com.longrise.android.byjk.advertisement.admanager.ADManager;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.common.MessageConstants;
import com.longrise.android.byjk.model.HomeMultipleItem;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.PersonalExhibitionActivity;
import com.longrise.android.byjk.plugins.login.TipsActivity;
import com.longrise.android.byjk.plugins.vip.VipType;
import com.longrise.android.byjk.plugins.vip.integralexchange.MyMedalRankHelper;
import com.longrise.common.base.BaseFragment;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DZZWTools;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.common.utils.NetUtil;
import com.longrise.common.utils.PrintLog;
import com.longrise.android.byjk.utils.Tracker_Google;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.android.byjk.widget.view.MarqueeView;
import com.longrise.android.by.activity.ClientManagerActivity;
import com.longrise.android.by.activity.XQPCActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by callxkj on 2017/2/21.
 * 首页-
 */

public class HomeFragment extends BaseFragment<HomePresenter> implements ViewPager.OnPageChangeListener,
        HomeContract.View, SwipeRefreshLayout.OnRefreshListener,
        HomeRcvheaderAdapter.OnHeadItemClickListener, HomeRcvAdapter.onItemTypeClickListener,
        View.OnClickListener {

    private static final String TAG = "HomeFragment";
    private boolean mFlag = true;
    private RecyclerView miRV;
    private LinearLayout mHeaderView = null;
    private ViewPager mViewPager = null;
    private LinearLayout mLinearLayout = null;
    private HomepagerAdapter mHomepagerAdapter = null;
    private int mLenght;
    private AutoSwitchPicTask mAutoSwitchTask = null;
    private RelativeLayout mRltitle;
    private ImageView mTitleScaniv;
    private float mBannerHeight;
    private ImageView mTitleIvbg;
    private RecyclerView mHead_rcv;
    private int mOverallYScrol = 0;
    private BBswipeRefreshLayout mSwipeRefreshLayout;
    private View mTitleDivider;
    private HomeRcvheaderAdapter mHead_rcv_adapter;
    private MarqueeView mMarqueeView;
    private HomeRcvAdapter homeRcvAdapter;
    private RelativeLayout mRl_scan;
    private HomeVipBrodcast mHomeVipBrodcast;
    private InterstitialAD interstitialAD;
    private boolean mCanShowAD = true;
    private LinearLayout mActivityLayout;
    private LinearLayout mAddressbookLayout;
    private boolean isHotUpdate = false; //检查热更新是否成功
    private UpdataAPK mUpdataAPK;
    private TextView mTvSign;
    private Dialog mSignDialog;

    private ImageView mHead_bxyx;
    private ImageView mHead_course;
    private RelativeLayout mRlbxyx;
    private RelativeLayout mRltk;
    private RelativeLayout mRlzsk;


    @Override
    protected int getLayoutResourse(@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home;
    }


    @Override
    public void initView() {
        miRV = (RecyclerView) mRootView.findViewById(R.id.home_fragment_irv);
        mSwipeRefreshLayout = (BBswipeRefreshLayout) mRootView.findViewById(R.id.home_fragment_swipelayout);
        mRltitle = (RelativeLayout) mRootView.findViewById(R.id.home_fragment_title_background);
        mTitleScaniv = (ImageView) mRootView.findViewById(R.id.home_fragment_title_scan_iv);
        mRl_scan = (RelativeLayout) mRootView.findViewById(R.id.home_fragment_title_scan_rl);
        mTitleIvbg = (ImageView) mRootView.findViewById(R.id.home_fragment_title_iv);
        mTitleDivider = mRootView.findViewById(R.id.home_fragment_title_divide);
        mTvSign = (TextView) mRootView.findViewById(R.id.home_fragment_title_tv_sign);

        //头部viewpager
        mHeaderView = (LinearLayout) View.inflate(mContext, R.layout.header_homeview, null);
        mViewPager = (ViewPager) mHeaderView.findViewById(R.id.homepage_header_vp);
        mHead_bxyx = (ImageView) mHeaderView.findViewById(R.id.item_homercv2_iv);
        mHead_course = (ImageView) mHeaderView.findViewById(R.id.item_homercv3_iv);

        mLinearLayout = (LinearLayout) mHeaderView.findViewById(R.id.homepage_header_point_container);
        //头部的动态配置按钮
        mHead_rcv = (RecyclerView) mHeaderView.findViewById(R.id.header_homview_rcy);
        //头部滚动文字
//        mMarqueeView = (MarqueeView) mHeaderView.findViewById(R.id.home_fragment_bbdt_MarqueeView);

        mRlbxyx = (RelativeLayout) mHeaderView.findViewById(R.id.home_head_tab_rl1);
        mRltk = (RelativeLayout) mHeaderView.findViewById(R.id.home_head_tab_rl2);
        mRlzsk = (RelativeLayout) mHeaderView.findViewById(R.id.home_head_tab_rl3);

        mHead_bxyx.setOnClickListener(this);
        mHead_course.setOnClickListener(this);
        initTitle();
        initVp();
        initHead_rcv();
        initHome_rcv();
        registBroadcast();

        mPresenter.init();
        mSwipeRefreshLayout.setRefreshing(true);
        // showTips(); 首页新手指引
        checkUpdate();

        checkHotUpdate();//检查热更新是否成功

        initMedalRank();
        Tracker_Google.getInstance().addGoogleScreenName("首页");
    }

    /**
     * 初始化标题
     */
    private void initTitle() {
        mTvSign.setOnClickListener(this);
        mRl_scan.setOnClickListener(this);
        mRlbxyx.setOnClickListener(this);
        mRltk.setOnClickListener(this);
        mRlzsk.setOnClickListener(this);
    }

    /**
     * 初始化头部的viewpager
     */
    private void initVp() {
        int width = AppUtil.getScreenWidth();
        mBannerHeight = (float)width * 170 / 375;
        ViewGroup.LayoutParams layoutParams = mViewPager.getLayoutParams();
        layoutParams.height = (int) mBannerHeight;
        mViewPager.setLayoutParams(layoutParams);
        mViewPager.setPageMargin(AppUtil.dip2px(12));
        mHomepagerAdapter = new HomepagerAdapter(mContext, new HomepagerAdapter.OnItemSelecter() {
            @Override
            public void onBananerClick(EntityBean bean) {
                if (bean != null) {
                    HomeViewJumpHelper.BannerJump(mContext, bean);
                }
            }
        });
        mViewPager.setAdapter(mHomepagerAdapter);
    }

    /**
     * 头部的动态配置按钮
     */
    private void initHead_rcv() {
        mHead_rcv_adapter = new HomeRcvheaderAdapter();
        mHead_rcv_adapter.setOnItemClickListener(this);
        mHead_rcv.setLayoutManager(new GridLayoutManager(mContext, 4));
        mHead_rcv.setAdapter(mHead_rcv_adapter);
    }

    /**
     * 初始化整体的recycleview
     */
    private void initHome_rcv() {
        mSwipeRefreshLayout.setOnRefreshListener(this);
        List<HomeMultipleItem> list = new ArrayList<>();
        homeRcvAdapter = new HomeRcvAdapter(list);
        miRV.setLayoutManager(new LinearLayoutManager(mContext));
        homeRcvAdapter.setHeaderView(mHeaderView);
        miRV.setAdapter(homeRcvAdapter);
        setTitleBgColor(0);
        miRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                mOverallYScrol = mOverallYScrol + dy;
                setTitleBgColor(mOverallYScrol);
            }
        });


        homeRcvAdapter.setOnItemTypeClickListener(this);
    }

    /**
     * 初始化我的勋章排名,用于个人资料里面的我的勋章
     */
    private void initMedalRank() {
        MyMedalRankHelper.reloadRank((Activity) mContext, false);
    }

    /**
     * 设置标题栏的颜色
     */
    private void setTitleBgColor(int space) {
        int titieHeight = AppUtil.dip2px(44);
        int restHeight = (int) (mBannerHeight - titieHeight);
        float scale = (float) space / restHeight;
        int alpha = (int) (scale * 255);
        if (scale <= 0.2) {
            mTitleDivider.setVisibility(View.GONE);
            mTitleIvbg.setBackgroundResource(R.drawable.bg_top_all);
            mTitleScaniv.setBackgroundResource(R.drawable.ic_home_scan_second);
            mTvSign.setTextColor(Color.parseColor("#FFFFFF"));
        } else if (scale > 0.2 && scale < 0.8) {
            mTitleDivider.setVisibility(View.GONE);
            mTitleIvbg.setBackgroundColor(Color.argb(alpha, 243, 243, 243));
            mTitleScaniv.setBackgroundResource(R.drawable.ic_home_scan_second);
            mTvSign.setTextColor(Color.parseColor("#FFFFFF"));
        } else {
            mTitleDivider.setVisibility(View.VISIBLE);
            mTitleIvbg.setBackgroundColor(Color.argb(255, 243, 243, 243));
            mTitleScaniv.setBackgroundResource(R.drawable.ic_home_scan_main);
            mTvSign.setTextColor(Color.parseColor("#FF9600"));
        }
    }

    /**
     * 注册广播 用于接收 vip页面发回的数据
     */
    private void registBroadcast() {
        if (mHomeVipBrodcast == null) {
            mHomeVipBrodcast = new HomeVipBrodcast();
        }
        IntentFilter intentFilter = new IntentFilter(MessageConstants.HOME_VIP_UPDATE);
        mContext.registerReceiver(mHomeVipBrodcast, intentFilter);
    }

    /**
     * 初始化插屏广告
     */
    private void initAD() {
        interstitialAD = ADManager.getInstance().createInterstitialAD(this.getActivity());
        interstitialAD.setInterstitialADListener(new InterstitialADListener() {
            @Override
            public void onSuccess() {
                showAD();
            }

            @Override
            public void onFail() {
                checkNotificationSwitch(); //没有广告则直接检查通知权限
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_fragment_title_scan_rl: //title右侧扫描按钮
                HomeViewJumpHelper.QRcodeJump(mContext);
                break;
            case R.id.home_fragment_title_tv_sign: //title左侧的签到按钮
                HomeViewJumpHelper.signJump(mContext);
                Tracker_Google.getInstance().addEvent("首页", "签到", "");
                break;
            case R.id.dialog_home_sign_wzdl_tv: //签到弹窗上的我知道了
                dismissSignDialog();
                Tracker_Google.getInstance().addEvent("每日签到弹窗", "我知道了", "");
                break;
            case R.id.dialog_home_sign_msqd_tv: //签到弹窗上的马上签到
                dismissSignDialog();
                HomeViewJumpHelper.signJump(mContext);
                Tracker_Google.getInstance().addEvent("每日签到弹窗", "立即签到", "");
                break;
            case R.id.tv_cancel_opennotification: //打开消息通知弹窗---暂不
                toCancelOpenNotification();
                Tracker_Google.getInstance().addEvent("开启推送弹窗", "暂不", "");
                break;
            case R.id.tv_confrim_opennotification: //打开消息通知弹窗---开启
                toConfirmOpenNotification();
                Tracker_Google.getInstance().addEvent("开启推送弹窗", "开启", "");
                break;

            case R.id.item_homercv2_iv: //保险医学  以后删除
                HomeViewJumpHelper.toBXYX(mContext);
                break;

            case R.id.item_homercv3_iv: //我的课程 以后删除
                HomeViewJumpHelper.toWDKC(mContext);
                break;

            case R.id.home_head_tab_rl1: //保险医学
                HomeViewJumpHelper.toBXYX(mContext);
                break;

            case R.id.home_head_tab_rl2: //题库
                HomeViewJumpHelper.toTK(mContext);
                break;

            case R.id.home_head_tab_rl3: //知识库
                HomeViewJumpHelper.toZSK(mContext);
                break;
        }
    }

    /**
     * 打开通知
     */
    private void toConfirmOpenNotification() {
        DialogUtil.getInstance().dismiss();
//        mPresenter.requestSignScoredToday(); //弹签到弹窗
        long currentTimeMillis = System.currentTimeMillis(); //无论是否开启 都需要做标记
        Control.setNotificationSwitchCloseTime(currentTimeMillis + "");
        Intent intent = new Intent(Settings.ACTION_SETTINGS);
        mContext.startActivity(intent);
    }

    /**
     * 没有打开通知
     */
    private void toCancelOpenNotification() {
        DialogUtil.getInstance().dismiss();
//        mPresenter.requestSignScoredToday(); //弹签到弹窗
        long currentTimeMillis = System.currentTimeMillis(); //无论是否开启 都需要做标记
        Control.setNotificationSwitchCloseTime(currentTimeMillis + "");
    }

    /**
     * 头部的自定义的按钮
     *
     * @param uniqueid   中间自定义按钮的唯一id
     * @param linkedtype 跳转原生页面2001 webview2002
     * @param text       按钮下面的文字
     */
    @Override
    public void onHeadItemClick(String uniqueid, String linkedtype, String text, String linkedurl) {
        if (NetUtil.checkNetEnable()) {
            HomeViewJumpHelper.btJump(mContext, uniqueid, linkedtype, text, linkedurl);
        }

    }


    /**
     * 刷新首页banner
     */
    @Override
    public void refreshBanner(EntityBean[] resultBean) {
        mLenght = resultBean.length;
        if (mAutoSwitchTask != null) {
            mAutoSwitchTask.stop();
            mAutoSwitchTask = null;
        }
        mHomepagerAdapter.setImageUrls(resultBean);
        mHomepagerAdapter.notifyDataSetChanged();
        initViewPager();
    }


    /**
     * 刷新头部的几个动态配置的按钮
     */
    @Override
    public void refreshHead_rcv(EntityBean[] resultBean) {
        ViewGroup.LayoutParams layoutParams = mHead_rcv.getLayoutParams();
        int itemHeight = AppUtil.dip2px(93);
        if (resultBean.length <= 4) {
            layoutParams.height = itemHeight;
        } else {
            layoutParams.height = itemHeight * 2;
        }
        mHead_rcv.setLayoutParams(layoutParams);
        mHead_rcv_adapter.setDatas(resultBean);
    }

    /**
     * 刷新新手专区,保宝头条这种item
     */
    @Override
    public void refreshHome_rcv(EntityBean[] beans, List<HomeMultipleItem> items) {
        homeRcvAdapter.setData(items, beans);
    }

//    /**
//     * 刷新保宝动态
//     *
//     * @param msgs 动态内容
//     */
//    @Override
//    public void refreshMarqueeView(EntityBean[] msgs) {
//        List<String> msgList = new ArrayList<>();
//        for (EntityBean bean : msgs) {
//            String title = bean.getString("title");
//            msgList.add(title);
//        }
//        mMarqueeView.startWithList(msgList);
//    }


    @Override
    public void refreshComplete() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    /**
     * 显示提示签到dialog
     *
     * @param acquiredscore 可以获取的分数
     * @param qdcounts      签到人数
     * @param planstate     //1在活动期间  0 不在活动期间
     */
    @Override
    public void showSignDialog(String acquiredscore, String qdcounts, String planstate) {
        View dialogView = View.inflate(mContext, R.layout.dialog_home_tosign, null);
        ImageView iv = (ImageView) dialogView.findViewById(R.id.dialog_home_sign_iv);
        TextView tv_wzdl = (TextView) dialogView.findViewById(R.id.dialog_home_sign_wzdl_tv);
        TextView tv_msqd = (TextView) dialogView.findViewById(R.id.dialog_home_sign_msqd_tv);
        TextView tv_jf = (TextView) dialogView.findViewById(R.id.dialog_sign_home_jf_tv);
        TextView tv_jf2 = (TextView) dialogView.findViewById(R.id.dialog_sign_home_jf_tv2);
        tv_wzdl.setOnClickListener(this);
        tv_msqd.setOnClickListener(this);
        TextView tv1 = (TextView) dialogView.findViewById(R.id.dialog_home_sign_tv1);
        TextView tv2 = (TextView) dialogView.findViewById(R.id.dialog_home_sign_tv2);
        if ("0".equals(planstate)) {
            tv_jf.setVisibility(View.VISIBLE);
            tv_jf2.setVisibility(View.VISIBLE);
            iv.setBackgroundResource(R.drawable.bg_home_alert_succes_normal);
            tv1.setText("您今天还未签到哦");
            tv_jf.setText("+" + acquiredscore);
        } else if ("1".equals(planstate)) {
            iv.setBackgroundResource(R.drawable.bg_home_alert_succes3);
            tv1.setText("今日签到 " + "+" + acquiredscore + " 积分");
        }
        if ("0".equals(qdcounts)) {
            tv2.setText("今日0位用户已签到，马上签到抢沙发");
        } else {
            tv2.setText("今天" + qdcounts + "位用户已签到");
        }
        mSignDialog = DialogUtil.getInstance().creatAlertDialog(mContext, dialogView, 270, 355);
        mSignDialog.show();
    }

    /**
     * 底部新手专区 或者宝宝头条的点击事件
     *
     * @param type       HomeMultipleItem.type1 HomeMultipleItem.type2
     * @param url        webview加载的地址
     * @param text       webview的title 可能使用
     * @param linkedtype
     */
    @Override
    public void onClick(int type, String url, String text, String linkedtype, String uniqueid) {
        HomeViewJumpHelper.customerModuleJump(mContext, type, url, text, linkedtype, uniqueid);
    }

    /**
     * 显示提示
     */
    private void showTips() {
        boolean homeFragmentTips = Control.isHomeFragmentTips();
        if (homeFragmentTips) {
            Intent intent = new Intent(mContext, TipsActivity.class);
            intent.putExtra("type", TipsActivity.HOME_FRAGMENT);
            mContext.startActivity(intent);
            Control.setIsHomeFragmentTips(false);
        }

    }

    private void dismissSignDialog() {
        if (mSignDialog != null && mSignDialog.isShowing()) {
            mSignDialog.dismiss();
        }
    }

    //---------------以下涉及viewPager重新初始化---------------------------------------------
    public void initViewPager() {
        mLinearLayout.removeAllViews(); // 如果有圆圈 全部先移除
        for (int i = 0; i < mLenght; i++) {
            // 动态的加载点
            View point = new View(getContext());
            point.setBackgroundResource(R.drawable.point_normal);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(AppUtil.dip2px(5), AppUtil.dip2px(5));

            if (i != 0) {
                params.leftMargin = AppUtil.dip2px(5);
            } else {
                // 设置默认值
                point.setBackgroundResource(R.drawable.point_selected);
                params.width = AppUtil.dip2px(15);
            }
            mLinearLayout.addView(point, params);
        }

        // 开启轮播图
        if (mAutoSwitchTask == null) {
            mAutoSwitchTask = new AutoSwitchPicTask();
        }
        mAutoSwitchTask.start();

        // 设置ViewPager的touch的监听
        mViewPager.setOnTouchListener(new View.OnTouchListener() {

            private int startx;

            private int starty;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                        // 如果手指按下去时，希望轮播停止，
                        // 如果手指按下去时，希望轮播停止，
                        if (mAutoSwitchTask != null) {
                            mAutoSwitchTask.stop();
                        }
                        startx = (int) event.getX();
                        starty = (int) event.getY();
                        break;

                    case MotionEvent.ACTION_MOVE:
                        int endx = (int) event.getX();
                        int endy = (int) event.getY();
                        if (Math.abs(endx - startx) > Math.abs(endy - starty)) {
                            mViewPager.getParent().requestDisallowInterceptTouchEvent(true);
                        }
                        break;

                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        // 如果手指抬起时，图片进行轮播
                        if (mAutoSwitchTask != null) {
                            mAutoSwitchTask.start();
                        }
                        break;

                    default:
                        break;
                }
                return false;
            }

        });
        mViewPager.addOnPageChangeListener(this);
        int middle = 1000 / 2;
        if (mLenght != 0) {
            int extra = middle % mLenght;
            mViewPager.setCurrentItem(middle - extra);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        try {
            position = position % mLenght;
            // 设置选中的点
            // 设置背景资源,清空已经选中的
            int count = mLinearLayout.getChildCount();
            for (int i = 0; i < count; i++) {
                View view = mLinearLayout.getChildAt(i);
                if (i == position) {
                    ViewGroup.LayoutParams selectlayoutParams = view.getLayoutParams();
                    selectlayoutParams.width = AppUtil.dip2px(15);
                    view.setLayoutParams(selectlayoutParams);
                } else {
                    ViewGroup.LayoutParams normallayoutParams = view.getLayoutParams();
                    normallayoutParams.width = AppUtil.dip2px(5);
                    view.setLayoutParams(normallayoutParams);
                }
                view.setBackgroundResource(i == position ? R.drawable.point_selected : R.drawable.point_normal);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    private class AutoSwitchPicTask extends Handler implements Runnable {
        public void start() {
            stop();
            postDelayed(this, 5000);
        }

        public void stop() {
            removeCallbacks(this);
        }

        @Override
        public void run() {
            int position = mViewPager.getCurrentItem();
            if (mViewPager.getAdapter() == null) {
                return;
            }
            if (position != mViewPager.getAdapter().getCount() - 1) {
                if (position >= 0) {
                    mViewPager.setCurrentItem(++position);
                }
            } else {
                mViewPager.setCurrentItem(0);
            }
            postDelayed(this, 5000);
        }
    }
//---------------以上涉及viewPager重新初始化-------------------------------------------------------

    /**
     * 监听当前页面是否在最上层
     */
    public void setMonitor(boolean flag) {
        mFlag = flag;
        if (mFlag) {
            showAD(); //如果广告还没显示过 切回home页面了就应该显示一次广告
        }
    }


    @Override
    public void onRefresh() {
        mPresenter.refreshData();
    }



    @Override
    public void showToast(String msg) {
        DZZWTools.showToast(mContext, msg, 1);
    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHomeVipBrodcast != null) {
            mContext.unregisterReceiver(mHomeVipBrodcast);
        }
    }

    /**
     * 接收vip页面返回的信息
     */
    private class HomeVipBrodcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && intent.getAction() == MessageConstants.HOME_VIP_UPDATE) {
                if (UserInfor.getInstance().getViptype() == VipType.VIP_3) {
                    switch (CustomerModuleJumpHelper.mCurrentVipOption) {
                        case CustomerModuleJumpHelper.ZYXPage:
                            HomeViewJumpHelper.jump(mContext, PersonalExhibitionActivity.class, null);
                            break;

                        case CustomerModuleJumpHelper.BYZPPage:
                            HomeViewJumpHelper.jump(mContext, XQPCActivity.class, null);
                            break;

                        case CustomerModuleJumpHelper.KHGLPage:
                            HomeViewJumpHelper.jump(mContext, ClientManagerActivity.class, null);
                            break;

                        default:
                            break;
                    }
                }
            }
        }
    }

    /**
     * 首页也要添加强更新提示
     */
    private void checkUpdate() {
        mUpdataAPK = new UpdataAPK(mContext, AppUtil.getString(R.string.leapAppName), true);
        mUpdataAPK.setCheckVersionListener(new UpdataAPK.CheckVersionListener() {
            @Override
            public void onCheckVersion(int mark) {
                if (mark == 0 || mark == -2) { //用户点击取消更新,或者app没有更新
                    // ,或者请求版本的接口错误 就进入app
                    initAD();
                }
            }
        });
        mUpdataAPK.checkVersion();
    }

    /**
     * 显示广告
     */
    private void showAD() {
        if (mFlag && mCanShowAD) { //只有当前页是home页的时候才能显示广告
            Dialog adDialog = interstitialAD.show();
            mCanShowAD = false; //广告只能显示一次
            if (adDialog == null) {
                return;
            }
            adDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    checkNotificationSwitch(); //有广告则等广告消失了再检查通知开关
                }
            });
        }
    }

    private void checkNotificationSwitch() {
//        boolean b = BbJpushHelper.ifRemindNotification(mContext);
//        if (b) {
//            showOpenNotificationDialog();
//        } else {
////            mPresenter.requestSignScoredToday(); //通知权限有的话,弹签到弹窗
//        }
    }

    /**
     * 打开消息推送的开关
     */
    private void showOpenNotificationDialog() {
        View dialogView = View.inflate(mContext, R.layout.dialog_opennotification, null);
        TextView tv_cancel = (TextView) dialogView.findViewById(R.id.tv_cancel_opennotification);
        TextView tv_confirm = (TextView) dialogView.findViewById(R.id.tv_confrim_opennotification);
        tv_cancel.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        DialogUtil.getInstance().creatAlertDialog(mContext, dialogView, 266, 171);
    }

    /**
     * 检查热更新是否成功
     */
    private void checkHotUpdate() {
        if (isHotUpdate) {
            addGoogleEvent("hot-update", "updated", "已更新");
            PrintLog.d(TAG, "已更新");
        } else {
            addGoogleEvent("hot-update", "not-updated", "未更新");
            PrintLog.d(TAG, "还未更新");
        }
    }

    private static void addGoogleEvent(String par1, String par2, String par3) {
        Tracker_Google.getInstance().addEvent(par1, par2, par3);
    }

}
